<!--首页-资产模块-->
<template>
  <div>
    <div :class="['bg2', apiId ? 'bg-api' : 'bg']" v-if="assets" @click="assetsClick">
      <LImage  class="img-bg" :src="Config.img('v2_shouye_bg.png')">></LImage>

      <div class="row1">
        <text class="font30 text2">{{T('总账户资产折合')}}</text>
        <div class="but-size" @click="eyeClick(!eye)">
          <LImage  class="imge-eye" :src="Config.img(eye?'v2_eye_yes.png':'v2_eye_no.png')">></LImage>
        </div>

        <div class="all"></div>
        <VRefresh class="but-size" :spinKey="'home_asset'" :rotate="rotate" image="v2_shuaxin.png" @click="refreshClick"></VRefresh>
      </div>

      <div class="row2 aend" v-if="eye">
        <text class="font48 text2">{{assets.totalBtc}}</text>
        <text class="font26 text2 text-up8">{{' BTC'}}</text>
        <div class="all"></div>     
        <text class="font26 text3 text-up8">{{T('昨日收益')+'('+Config.priceUnit+')'}}</text>
      </div>
      <div class="row2 aend" v-else>
        <text class="font48 text2 text-down16">{{'******'}}</text>
        <text class="font26 text2 text-up8">{{' BTC'}}</text>
        <div class="all"></div>            
        <text class="font26 text3 text-up8">{{T('昨日收益')+'('+Config.priceUnit+')'}}</text>
      </div>
      
      <div class="height6"></div>
      <div class="row3" v-if="eye">
        <text class="font30 text3">{{'≈'+ assets.totalMoney+' '+Config.priceUnit}}</text>    
        <div class="all"></div>     
        <text class="font24 text1">{{assets.yesterdayEarning}}</text>
      </div>
      <div class="row3" v-else>
        <text class="font30 text3">{{'≈'}}</text>  
        <text class="font30 text3 text-down20">{{'******'}}</text>  
        <text class="font30 text3">{{' '+Config.priceUnit}}</text>  
        <div class="all"></div>       
        <text class="font24 text1">{{'******'}}</text>
      </div>
    </div>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.bg {
  height: 258px;
  border-radius: 24px;
  padding: 40px;
}
.bg-api {
  height: 258px;
  border-bottom-left-radius: 24px;
  border-bottom-right-radius: 24px;
  padding: 40px;
}
.img-bg {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}

/************ 第1行 */
.row1{
  flex-direction: row;
  align-items: center;
  height: 100px;

  margin-top: -30px;
  margin-right: -15px;
}
.but-size {
  justify-content: center;
  align-items: center;
  width: 62px;
  height: 62px;
}
.imge-eye {
  width: 44px;
  height: 32px;
}

/************ 第2行 */
.row2 {
  flex-direction: row;
  height: 60px;
}
.text-up8{
  margin-bottom: 8px;
}
.text-down16{
  margin-bottom: -16px;
}

/************ 第3行 */
.row3 {
  flex-direction: row;
  align-items: center;
  height: 40px;
}
.text-down20 {
  margin-bottom: -14px;
}
/********************************** 间距*/
.height6 {
  height: 6px;
}
</style>

<script>
//组件
import VRefresh from "@/home/view/VRefresh"

//功能
import jmath from "@/_js/math";
import jconfig from "@/_js/config";
import japi from "@/_js/api";
import jdebug from "@/_js/debug";


const weexFunction = weex.requireModule('WeexFunction');
const globalEvent = weex.requireModule('globalEvent');

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {
  
  components: {
    VRefresh,
  },

  
  model:{
    prop:"eye",
    event:"change",
  },

  props:{
    eye:{
      type:Boolean,
      default:true, //true 显示资产
    },

    apiId:{
      type:String,
      default:undefined,
    },
  },

  data() {
    return {
      T:T,
      Config:jconfig,
      rotate:false, //true 刷新
      assets:undefined, //资产
    };
  },

  created() {
    jconfig.init(()=>{
      this.initData();
    },[jconfig.OPT_UP_DOWN]);


    weexFunction.getConfig("assets_eye",(value)=>{
      var eye = value == "true";
      this.changeEye(eye);
    });

    //Api更新通知
    globalEvent.addEventListener("apiUp",(e)=>{
      this.initData();
    });
    
  },

  methods: {
    
    initData() {

      /*截取*/
      var guiFan = (assets)=>{

        //总资产折合-BTC
        assets.totalBtc = jmath.gfCoin(assets.totalBtc, 'BTC', true);
        
        //总资产折合-钱
        var totalMoney = jconfig.priceUnit == 'USD' ? assets.totalUsd : assets.totalCny;
        assets.totalMoney = jmath.gfMoney(totalMoney, true);

        //昨日策略收益-
        var yesterdayEarning = jconfig.priceUnit == 'CNY' ? assets.yesterdayEarningCny : assets.yesterdayEarningUsd;
        yesterdayEarning = jmath.gfMoney(yesterdayEarning, true);;
        yesterdayEarning = jmath.addJia(yesterdayEarning);
        assets.yesterdayEarning = yesterdayEarning;

        return assets;
      }

      if (this.apiId) {
        japi.getApi(this.apiId, 'all', (api)=>{
          weexFunction.setTitle(api.apiName);
          api = guiFan(api); //API
          this.assets = api;
        });
      } else {
        japi.getTotalAssets((assets)=>{
          this.assets = guiFan(assets); //总账户
        });
      }
    },
    
    //眼睛改变
    changeEye(eye) {
      this.eye = eye;
      this.$emit("change", this.eye); 
    },

    /********************************** 点击触发*/
    //资产点击
    assetsClick(){
      this.$emit("click");
    },

    //眼睛
    eyeClick(eye){
      this.changeEye(eye);
      var value = eye+'';
      weexFunction.setConfig("assets_eye", value);
    },

    //刷新
    refreshClick(){
      if (this.rotate) {
        return;
      }

      this.rotate = true;

      if (this.apiId) {

        japi.synchroApi(this.apiId, ()=>{
          this.rotate = false;
        }, ()=>{
          this.rotate = false;
        });
      } else {

        japi.synchroApis(()=>{
          this.rotate = false;
        }, ()=>{
          this.rotate = false;
        });
      }
    },
  }
}
</script>